<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="static/elementui.css">
    <script src="static/vue@2.6.14.js"></script>
    <script src="static/elementui.js"></script>
    <script src="static/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            width: 100vw;
            height: 100vh;
            overflow: hidden;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            position: relative;
        }

        .video-background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 1;
        }

        .video-background video {
            width: 100%;
            height: 100%;
            object-fit: cover;
            position: absolute;
            top: 0;
            left: 0;
        }

        .page-container {
            position: relative;
            width: 100%;
            height: 100%;
            z-index: 2;
            display: flex;
        }

        .login-section {
            width: 500px;
            height: 100%;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 40px;
            position: relative;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .login-container {
            max-width: 400px;
            margin: 0 auto;
            width: 100%;
        }

        .login-header {
            font-size: 28px;
            color: #1a1a1a;
            margin-bottom: 40px;
            font-weight: 600;
            position: relative;
        }

        .login-header::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 40px;
            height: 3px;
            background: #0065ff;
        }

        .el-card {
            background: transparent;
            border: none;
            box-shadow: none !important;
        }

        .el-tabs__item {
            font-size: 16px;
            color: #666;
            padding: 0 20px;
            height: 50px;
            line-height: 50px;
        }

        .el-tabs__item.is-active {
            color: #0065ff;
            font-weight: 500;
        }

        .el-tabs__active-bar {
            background-color: #0065ff;
            height: 3px;
        }

        .el-form-item__label {
            color: #333;
            font-weight: 500;
            padding-bottom: 8px;
            line-height: 1;
        }

        .el-input__inner {
            height: 45px;
            line-height: 45px;
            border-radius: 8px;
            border: 1px solid rgba(0, 101, 255, 0.2);
            padding: 0 15px;
            font-size: 14px;
            transition: all 0.3s;
            background: rgba(255, 255, 255, 0.8);
        }

        .el-input__inner:focus {
            border-color: #0065ff;
            box-shadow: 0 0 0 2px rgba(0, 101, 255, 0.1);
            background: rgba(255, 255, 255, 0.95);
        }

        .el-button--primary {
            width: 100%;
            height: 45px;
            border-radius: 8px;
            background: #0065ff;
            border: none;
            font-size: 16px;
            font-weight: 500;
            letter-spacing: 1px;
            transition: all 0.3s;
        }

        .el-button--primary:hover {
            background: #0052cc;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 101, 255, 0.2);
        }

        .avatar-uploader .el-upload {
            width: 100px;
            height: 100px;
            border: 2px dashed rgba(0, 101, 255, 0.2);
            border-radius: 12px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: all 0.3s;
            background: rgba(255, 255, 255, 0.8);
        }

        .avatar-uploader .el-upload:hover {
            border-color: #0065ff;
            background: rgba(0, 101, 255, 0.05);
        }

        .avatar {
            width: 100px;
            height: 100px;
            border-radius: 12px;
            object-fit: cover;
        }

        .captcha-wrapper {
            display: flex;
            gap: 15px;
            align-items: flex-start;
        }

        .captcha-wrapper .el-input {
            flex: 1;
        }

        .captcha-image {
            height: 45px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s;
            background: white;
        }

        .captcha-image:hover {
            opacity: 0.8;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #0065ff;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }

        ::-webkit-scrollbar {
            width: 6px;
        }

        ::-webkit-scrollbar-thumb {
            background-color: rgba(0, 101, 255, 0.2);
            border-radius: 3px;
        }

        ::-webkit-scrollbar-track {
            background-color: transparent;
        }
    </style>
</head>
<body>
<div class="video-background">
    <video autoplay muted loop playsinline>
        <source src="static/bg.mp4" type="video/mp4">
    </video>
</div>
<div class="page-container">
    <section class="login-section">
        <div id="app" class="login-container">
            <div class="login-header">欢迎登录</div>
            <el-card>
                <el-tabs v-model="activeTab">
                    <el-tab-pane label="登录" name="login">
                        <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="80px">
                            <el-form-item label="用户名" prop="username">
                                <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
                            </el-form-item>
                            <el-form-item label="验证码" prop="captcha">
                                <div class="captcha-wrapper">
                                    <el-input v-model="loginForm.captcha" placeholder="请输入验证码"></el-input>
                                    <img :src="captchaSrc" @click="generateCaptcha" class="captcha-image"/>
                                </div>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="handleLogin">登录</el-button>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane><el-tab-pane label="注册" name="register">
                    <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="80px">
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="confirmPassword">
                            <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请确认密码"></el-input>
                        </el-form-item>
                        <el-form-item label="头像">
                            <el-upload
                                    class="avatar-uploader"
                                    action="/upload"
                                    :show-file-list="false"
                                    :on-success="handleAvatarSuccess"
                                    :before-upload="beforeAvatarUpload">
                                <img v-if="registerForm.avatar" :src="registerForm.avatar" class="avatar"/>
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleRegister">注册</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                    <el-tab-pane label="忘记密码" name="forgotPassword">
                        <el-form :model="forgotPasswordForm" :rules="forgotPasswordRules" ref="forgotPasswordForm" label-width="80px">
                            <el-form-item label="用户名" prop="username">
                                <el-input v-model="forgotPasswordForm.username" placeholder="请输入用户名"></el-input>
                            </el-form-item>
                            <el-form-item label="新密码" prop="newPassword">
                                <el-input type="password" v-model="forgotPasswordForm.newPassword" placeholder="请输入新密码"></el-input>
                            </el-form-item>
                            <el-form-item label="确认密码" prop="confirmNewPassword">
                                <el-input type="password" v-model="forgotPasswordForm.confirmNewPassword" placeholder="请确认新密码"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="handleForgotPassword">重置密码</el-button>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane>
                </el-tabs>
            </el-card>
        </div>
    </section>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                activeTab: 'login',
                loginForm: {
                    username: '',
                    password: '',
                    captcha: ''
                },
                registerForm: {
                    username: '',
                    password: '',
                    confirmPassword: '',
                    avatar: ''
                },
                forgotPasswordForm: {
                    username: '',
                    newPassword: '',
                    confirmNewPassword: ''
                },
                loginRules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                    captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}]
                },
                registerRules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                    confirmPassword: [
                        {required: true, message: '请确认密码', trigger: 'blur'},
                        {
                            validator: (rule, value, callback) => {
                                if (value !== this.registerForm.password) {
                                    callback(new Error('两次输入的密码不一致'));
                                } else {
                                    callback();
                                }
                            },
                            trigger: 'blur'
                        }
                    ]
                },
                forgotPasswordRules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    newPassword: [{required: true, message: '请输入新密码', trigger: 'blur'}],
                    confirmNewPassword: [
                        {required: true, message: '请确认新密码', trigger: 'blur'},
                        {
                            validator: (rule, value, callback) => {
                                if (value !== this.forgotPasswordForm.newPassword) {
                                    callback(new Error('两次输入的密码不一致'));
                                } else {
                                    callback();
                                }
                            },
                            trigger: 'blur'
                        }
                    ]
                },
                captchaSrc: '',
                captchaText: ''
            }
        },
        created() {
            this.generateCaptcha();
        },
        methods: {
            generateCaptcha() {
                const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                let captcha = '';
                for (let i = 0; i < 6; i++) {
                    captcha += chars[Math.floor(Math.random() * chars.length)];
                }
                this.captchaText = captcha;
                const canvas = document.createElement('canvas');
                canvas.width = 100;
                canvas.height = 40;
                const ctx = canvas.getContext('2d');

                // 设置渐变背景
                const gradient = ctx.createLinearGradient(0, 0, 100, 40);
                gradient.addColorStop(0, '#f0f5ff');
                gradient.addColorStop(1, '#e6f0ff');
                ctx.fillStyle = gradient;
                ctx.fillRect(0, 0, 100, 40);

                // 绘制文字
                ctx.font = 'bold 24px Arial';
                ctx.fillStyle = '#0065ff';
                ctx.textBaseline = 'middle';
                ctx.textAlign = 'center';
                ctx.fillText(captcha, 50, 20);

                // 添加干扰线
                for (let i = 0; i < 3; i++) {
                    ctx.beginPath();
                    ctx.moveTo(Math.random() * 100, Math.random() * 40);
                    ctx.lineTo(Math.random() * 100, Math.random() * 40);
                    ctx.strokeStyle = 'rgba(0, 101, 255, 0.3)';
                    ctx.stroke();
                }

                this.captchaSrc = canvas.toDataURL();
            },
            handleAvatarSuccess(response, file) {
                if (response.code === 200) {
                    this.registerForm.avatar = response.data;
                    this.$message({
                        message: '头像上传成功',
                        type: 'success'
                    });
                } else {
                    this.$message.error('头像上传失败');
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 5;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isJPG && isLt2M;
            },
            handleLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        if (this.loginForm.captcha.toLowerCase() !== this.captchaText.toLowerCase()) {
                            this.$message.error('验证码错误');
                            this.generateCaptcha();
                            return;
                        }

                        axios.post('/user?operation=login', this.loginForm)
                            .then(response => {
                                if (response.data.code === 200) {
                                    this.$message({
                                        message: '登录成功',
                                        type: 'success',
                                        duration: 1500
                                    });
                                    localStorage.setItem("user", JSON.stringify(response.data.data));
                                    setTimeout(() => {
                                        location.href = "welcome.jsp";
                                    }, 1500);
                                } else {
                                    this.$message.error(response.data.msg);
                                    this.generateCaptcha();
                                }
                            })
                            .catch(() => {
                                this.$message.error('登录失败，请重试');
                                this.generateCaptcha();
                            });
                    }
                });
            },
            handleRegister() {
                this.$refs.registerForm.validate((valid) => {
                    if (valid) {
                        axios.post('/user?operation=register', this.registerForm)
                            .then(response => {
                                if (response.data.code === 200) {
                                    this.registerForm = {
                                        username: '',
                                        password: '',
                                        confirmPassword: '',
                                        avatar: ''
                                    };
                                    this.$message({
                                        message: '注册成功',
                                        type: 'success',
                                        duration: 1500
                                    });
                                    setTimeout(() => {
                                        this.activeTab = 'login';
                                    }, 1500);
                                } else {
                                    this.$message.error(response.data.msg);
                                }
                            })
                            .catch(() => {
                                this.$message.error('注册失败，请重试');
                            });
                    }
                });
            },
            handleForgotPassword() {
                this.$refs.forgotPasswordForm.validate((valid) => {
                    if (valid) {
                        axios.post('/user?operation=resetPassword', this.forgotPasswordForm)
                            .then(response => {
                                if (response.data.code === 200) {
                                    this.forgotPasswordForm = {
                                        username: '',
                                        newPassword: '',
                                        confirmNewPassword: ''
                                    };
                                    this.$message({
                                        message: '密码重置成功',
                                        type: 'success',
                                        duration: 1500
                                    });
                                    setTimeout(() => {
                                        this.activeTab = 'login';
                                    }, 1500);
                                } else {
                                    this.$message.error(response.data.msg);
                                }
                            })
                            .catch(() => {
                                this.$message.error('重置密码失败，请重试');
                            });
                    }
                });
            }
        }
    });
</script>
</body>
</html>